package com.cloudsigns.factorymanager2.engineering.adapter;

import android.content.ClipData;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringDailyFeedBackBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.project.adapter.EngineeringOrderListAdapter;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/4/10.
 */

public class DailyFeedBackForEngineeringInstallAdapter<T> extends BaseAdapter {

    private Context context;
    private List<T> datas = new ArrayList<T>();
    private LayoutInflater inflater;
    private ItemClickListener itemClickListener;
    private String TAG = "";


    public DailyFeedBackForEngineeringInstallAdapter(Context context, String TAG) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.TAG = TAG;

    }

    //    EngineeringDailyFeedBackBean
    public void setData(List<T> list) {

        if (null != list) {
            this.datas.clear();
            this.datas.addAll(list);
        }

    }

    public void setItemClick(ItemClickListener clickListener) {
        this.itemClickListener = clickListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public T getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_daily_feedbak_for_engineeringinstall, null);
            holder.tv_type = convertView.findViewById(R.id.tv_type);
            holder.tv_status = convertView.findViewById(R.id.tv_status);
            holder.img_arrow_right = convertView.findViewById(R.id.img_arrow_right);
            holder.img_no_feeedback = convertView.findViewById(R.id.img_no_feeedback);
            holder.rl_item = convertView.findViewById(R.id.rl_item);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (null != TAG) {
            if (TAG.equals("PatrolCompletionOrderForManagerActivity")) {
                MediaBean bean = (MediaBean) datas.get(position);
                holder.rl_item.setTag(bean);
                holder.tv_type.setText(bean.getAssignReceiveAccountName());

                CommonEnum.AssignStatus assignStatus = bean.getAssignStatus();
                if (null != assignStatus) {
                    if (assignStatus == CommonEnum.AssignStatus.CREATED) {
                        holder.tv_status.setText("[创建/未编辑]");
                    } else if (assignStatus == CommonEnum.AssignStatus.EDITED) {
                        holder.tv_status.setText("[已编辑/未派发]");

                    } else if (assignStatus == CommonEnum.AssignStatus.SEND) {
                        holder.tv_status.setText("[派发中]");

                    } else if (assignStatus == CommonEnum.AssignStatus.RECEIVED) {
                        holder.tv_status.setText("[已经接收]");

                    } else if (assignStatus == CommonEnum.AssignStatus.RETURNED) {
                        holder.tv_status.setText("[退回]");

                    } else if (assignStatus == CommonEnum.AssignStatus.COMPLETED) {
                        holder.tv_status.setText("[竣工]");

                    }
                }

                holder.img_arrow_right.setVisibility(View.VISIBLE);
            } else {
                EngineeringDailyFeedBackBean bean = (EngineeringDailyFeedBackBean) datas.get(position);
                holder.rl_item.setTag(bean);

                if (bean.isNoFeedBack()) {
                    holder.tv_type.setText("未反馈");
                    holder.img_no_feeedback.setVisibility(View.VISIBLE);
                    holder.img_arrow_right.setVisibility(View.GONE);
                } else {
                    holder.img_no_feeedback.setVisibility(View.GONE);
                    holder.img_arrow_right.setVisibility(View.VISIBLE);

                    CommonEnum.FeedBackType type = bean.getType();
                    if (null != type) {
                        if (type == CommonEnum.FeedBackType.NORMAL) {
                            holder.tv_type.setText("正常反馈");
                            String handlerAccountId = bean.getHandlerAccountId();
                            if (null != handlerAccountId && !TextUtils.isEmpty(handlerAccountId) && !handlerAccountId.equals("null")) {
                                holder.tv_status.setText("[已经确认]");
                                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_74BF73));
                            } else {
                                holder.tv_status.setText("[ 等待确认]");
                                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
                            }
//                            String status = bean.getStatus();
//                           if (null != status && !TextUtils.isEmpty(status) && !status.equals("null"))
//                                holder.tv_status.setText("[" + status + "]");
                        } else if (type == CommonEnum.FeedBackType.UNUSUAL) {
//                            holder.tv_type.setText("异常反馈");

                            CommonEnum.UnusualStatus unusualStatus = bean.getUnusualStatus();
                            if (null != unusualStatus) {
                                if (unusualStatus == CommonEnum.UnusualStatus.WAIT_CONFIRM) {
                                    holder.tv_status.setText("[ 等待确认]");
                                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
                                } else if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY) {
                                    holder.tv_status.setText("[等待回复]");
                                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
                                } else if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY_CONFIRM) {
                                    holder.tv_status.setText("[等待确认]");
                                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
                                } else if (unusualStatus == CommonEnum.UnusualStatus.REPLY_CONFIRM_FINISH) {
                                    holder.tv_status.setText("[已经确认]");
                                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_74BF73));
                                }
                            }

                            CommonEnum.SubType subType = bean.getSubType();
                            if (null != subType) {
                                if (subType == CommonEnum.SubType.DAMAGE) {
                                    holder.tv_type.setText("物流货损");
                                } else if (subType == CommonEnum.SubType.DELAY) {
                                    holder.tv_type.setText("物流延误");
                                } else if (subType == CommonEnum.SubType.INSTALL_UNUSUAL) {
                                    holder.tv_type.setText("安装异常");
                                } else if (subType == CommonEnum.SubType.OMIT) {
                                    holder.tv_type.setText("物流漏货");
                                }
                            }

                        } else if (type == CommonEnum.FeedBackType.COMPLETED) {
                            holder.tv_type.setText("完工反馈");
                            String handlerAccountId = bean.getHandlerAccountId();
                            if (null != handlerAccountId && !TextUtils.isEmpty(handlerAccountId) && !handlerAccountId.equals("null")) {
                                holder.tv_status.setText("[已经确认]");
                                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_74BF73));
                            } else {
                                holder.tv_status.setText("[ 等待确认]");
                                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
                            }
//
//                            String status = bean.getStatus();
//                            if (null != status && !TextUtils.isEmpty(status) && !status.equals("null"))
//                                holder.tv_status.setText("[" + status + "]");
                        }

                    }


                }

            }
        }

        holder.rl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != itemClickListener) {
                    itemClickListener.itemClick(view, position);
                }
            }
        });

        return convertView;
    }

    private class ViewHolder {
        private TextView tv_type;
        private TextView tv_status;
        private ImageView img_arrow_right;
        private ImageView img_no_feeedback;
        private View rl_item;
    }
}
